package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseSystemOperationLogMapper;
import com.redoor.app.stake.basicdata.model.BaseSystemOperationLog;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseSystemOperationLogCriteria;
import com.redoor.app.stake.basicdata.service.BaseSystemOperationLogService;

import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.utils.IpUtil;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-08 17:30:08.
 */
@Service
public class BaseSystemOperationLogServiceImpl extends AbstractService<BaseSystemOperationLog, String> implements BaseSystemOperationLogService {

    @Autowired
    private BaseSystemOperationLogMapper baseSystemOperationLogMapper;

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private LogService logService;

    @Override
    public void addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE web_operator_type, BussinessTypeEnum.TAG_TYPE action_type, String targetuuid, String remark,boolean intoReids) {
        BaseSystemOperationLog baseSystemOperationLog = new BaseSystemOperationLog();
        baseSystemOperationLog.setActiontype(action_type.getName());
        BaseUser user = baseUserService.getShiroUser(null);
        baseSystemOperationLog.setCreatoruuid(user!=null?user.getUuid():"-1");
        baseSystemOperationLog.setOpertorname(user!=null?user.getLoginname():"system");
        baseSystemOperationLog.setRemark(remark);
        baseSystemOperationLog.setOperatortype(web_operator_type.name());
        baseSystemOperationLog.setTargetuuid(targetuuid);
        baseSystemOperationLog.setUuid(UUIDTool.getUUID());
        baseSystemOperationLog.setOpertordate(new Date());
        try{
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if(null != requestAttributes){
                HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
                baseSystemOperationLog.setClientip(IpUtil.getIpAddr(request));
            }

            baseSystemOperationLog.setServerip(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ex){

        }

        if(intoReids){
            logService.addLogToRedis(baseSystemOperationLog, LogEnum.WEB_LOG);
        } else {
            this.save(baseSystemOperationLog);
        }
    }

    @Override
    public int saveAll(List<BaseSystemOperationLog> logList) {
        if(logList == null || logList.size() == 0){
            return 0;
        }
        baseSystemOperationLogMapper.batchInsert(logList);
        return 1;
    }

    @Override
    public List<BaseSystemOperationLog> qrySystemOperationLogByContion(BaseSystemOperationLogCriteria params) {
        return baseSystemOperationLogMapper.qrySystemOperationLog(params);
    }

    @Override
    public int qrySystemOperationLogCnt(BaseSystemOperationLogCriteria params) {
        return baseSystemOperationLogMapper.qrySystemOperationLogCnt(params);
    }
}
